package com.lottoad.android.activity.mypage.adapter;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lottoad.android.R;
import com.lottoad.android.cronos.data.CNLottoAdChangeListData;
import com.lottoad.android.cronos.datahandler.CNBaseDataSet;
import com.lottoad.android.util.ApplicationUtils;
import com.lottoad.android.util.RecycleUtils;

public class LottoAdChangeListAdapter extends BaseAdapter{

	protected LayoutInflater mInflater = null;
	protected ArrayList<CNBaseDataSet> mDataSetList = null;
	
	private List<WeakReference<View>> mRecycleList = new ArrayList<WeakReference<View>>();
	
	public void recycle() {
		for (WeakReference<View> ref : mRecycleList) {
			RecycleUtils.recursiveRecycle(ref.get());
		}
	}
	
	private class ViewHolder {
		TextView mNumTextView;
		TextView mAskDateTextView;
		TextView mPaidDateTextView;
		TextView mMoneyTextview;
		TextView mChangeTextView;
	}
	
	public LottoAdChangeListAdapter(Context context){
		this.mInflater = LayoutInflater.from(context);
	}
	
	public void setDataSetList(ArrayList<CNBaseDataSet> dataSetList) {
		this.mDataSetList = dataSetList;
	}
	
	public void clear(){
		if(this.mDataSetList != null){
			this.mDataSetList.clear();
			this.notifyDataSetChanged();
		}
	}
	
	@Override
	public int getCount() {
		if (this.mDataSetList != null) {
			return this.mDataSetList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (this.mDataSetList != null) {
			return this.mDataSetList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			convertView = this.mInflater.inflate(R.layout.lottoad_menu_mypage_changelist_row, null);
			holder = new ViewHolder();
			
			holder.mNumTextView = (TextView) convertView.findViewById(R.id.textView_num);
			holder.mAskDateTextView = (TextView) convertView.findViewById(R.id.textView_askDate);
			holder.mPaidDateTextView = (TextView) convertView.findViewById(R.id.textView_paidDate);
			holder.mMoneyTextview = (TextView) convertView.findViewById(R.id.textView_money);
			holder.mChangeTextView = (TextView) convertView.findViewById(R.id.textView_change);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		CNLottoAdChangeListData dataSet = (CNLottoAdChangeListData) getItem(position);
		
		if(dataSet != null){
			
			holder.mNumTextView.setText(String.valueOf(getCount() - position));
			
			holder.mAskDateTextView.setText(dataSet.askDate.split(" ")[0].substring(2, dataSet.askDate.split(" ")[0].length()));
			holder.mAskDateTextView.setPaintFlags(holder.mAskDateTextView.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG);
			
			if(dataSet.paidDate !=null &&  !dataSet.paidDate.equals("")&& dataSet.paidDate.length() > 0){
				holder.mPaidDateTextView.setText(dataSet.paidDate.split(" ")[0].substring(2, dataSet.paidDate.split(" ")[0].length()));
			} else {
				holder.mPaidDateTextView.setText("");
			}
			holder.mPaidDateTextView.setPaintFlags(holder.mPaidDateTextView.getPaintFlags() | Paint.FAKE_BOLD_TEXT_FLAG);
			
			holder.mMoneyTextview.setText(ApplicationUtils.makeStringWithComma(dataSet.askMoney,false) + " 원");
			holder.mChangeTextView.setText(dataSet.isChange);
		}
		
		return convertView;
	}
}
